﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW
{
    public partial class SearchMaintainRecords : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProducts();
                InitialDropDown();
                BindApplicatonEachMonth();
            }
        }

        private void BindApplicatonEachMonth()
        {
            ApplicationService service = new ApplicationService();
            IQueryable<ApplicationForm> forms = service.GetApplicationsByYear(Convert.ToInt32(yearDropDownList.SelectedValue));
            int jan = GetNumberOfForms(forms, 1);
            int fab = GetNumberOfForms(forms, 2);
            int mar = GetNumberOfForms(forms, 3);
            int apr = GetNumberOfForms(forms, 4);
            int may = GetNumberOfForms(forms, 5);
            int jun = GetNumberOfForms(forms, 6);
            int jul = GetNumberOfForms(forms, 7);
            int aug = GetNumberOfForms(forms, 8);
            int seq = GetNumberOfForms(forms, 9);
            int qct = GetNumberOfForms(forms, 10);
            int nov = GetNumberOfForms(forms, 11);
            int des = GetNumberOfForms(forms, 12);

            float totalNumber = jan + fab + mar + apr + may + jun + jul + aug + seq + qct + nov + des;
            Label1.Text = jan.ToString();
            Label2.Text = fab.ToString();
            Label3.Text = mar.ToString();
            Label4.Text = apr.ToString();
            Label5.Text = may.ToString();
            Label6.Text = jun.ToString();
            Label7.Text = jul.ToString();
            Label8.Text = aug.ToString();
            Label9.Text = seq.ToString();
            Label10.Text = qct.ToString();
            Label11.Text = nov.ToString();
            Label12.Text = des.ToString();

            Label13.Text = GetPercent(jan, totalNumber);
            Label14.Text = GetPercent(fab, totalNumber);
            Label15.Text = GetPercent(mar, totalNumber);
            Label16.Text = GetPercent(apr, totalNumber);
            Label17.Text = GetPercent(may, totalNumber);
            Label18.Text = GetPercent(jun, totalNumber);
            Label19.Text = GetPercent(jul, totalNumber);
            Label20.Text = GetPercent(aug, totalNumber);
            Label21.Text = GetPercent(seq, totalNumber);
            Label22.Text = GetPercent(qct, totalNumber);
            Label23.Text = GetPercent(nov, totalNumber);
            Label24.Text = GetPercent(des, totalNumber);

            string chartURL = "http://chart.apis.google.com/chart?chs=400x200&amp;chd=t:";
            chartURL += jan * 400 / totalNumber + "," + fab * 400 / totalNumber + "," + mar * 400 / totalNumber + "," + apr * 400 / totalNumber +"," +may*400/totalNumber + "," + jun * 400 / totalNumber + "," + jul * 400 / totalNumber+","+aug*400/totalNumber;
            chartURL += "," + seq * 400 / totalNumber + "," + qct * 400 / totalNumber + "," + nov * 400 / totalNumber + "," + des * 400 / totalNumber;
            chartURL += "&amp;cht=lc&amp;chxt=x,y&amp;chxl=0:|1月|2月|3月|4月|5月|6月|7月|8月|9月|10月|11月|12月|1:||数量";
            eachMothChart.Src = chartURL;
            //eachMothChart.Src = "http://chart.apis.google.com/chart?chs=200x125&amp;chd=s:helloWorld&amp;cht=lc&amp;chxt=x,y&amp;chxl=0:|Mar|Apr|May|June|July|1:||50+Kb";

            //bind top 10
            ApplicationProductService productservice = new ApplicationProductService();
            List<ProductTypeMaintainModel> ptmms = productservice.GetProdcutMaintainByProductType(Convert.ToInt32(yearDropDownList.SelectedValue));
            ptmms = ptmms.Where(p => p.Number > 0).OrderBy(p => p.Number).ToList();
            if(ptmms.Count > 10)
            {
                ptmms = ptmms.OrderByDescending(p=>p.Number).Take(10).ToList();
            }
            maintainProductRepeater.DataSource = ptmms;
            maintainProductRepeater.DataBind();

            int toptenNumber = 0;
            foreach(ProductTypeMaintainModel ptmm in ptmms)
            {
                toptenNumber+=ptmm.Number;
            }

            totalLabel.Text = ptmms[0].SumNumber.ToString();
            topTenLabel.Text = toptenNumber.ToString();
            tipTenPercentLabel.Text = (Math.Round(toptenNumber * 100 / Convert.ToDouble(ptmms[0].SumNumber),2)).ToString() + "%";
        }

        private string GetPercent(int month, float totalNumber)
        {
            return (Math.Round(month * 100 / Convert.ToDouble(totalNumber), 2)).ToString() + "%";
        }

        private int GetNumberOfForms(IQueryable<ApplicationForm> forms, int month)
        {
            return forms.Where(a => a.ApplicationDateTime.Value.Month == month).Count();
        }

        private void InitialDropDown()
        {
            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            yearDropDownList.SelectedValue = DateTime.Now.Year.ToString();
        }

        private void BindProducts()
        {
            ApplicationProductService service = new ApplicationProductService();
            productGridView.DataSource = service.GetProductByStartDateAndEndDate(startTextBox.Text, endTextBox.Text);
            productGridView.DataBind();
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            BindProducts();
        }

        protected void returnToFactoryFormsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < productGridView.PageCount)
            {
                productGridView.PageIndex = e.NewPageIndex;
            }
            
            BindProducts();
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void viewLinkButton_Click(object sender, EventArgs e)
        {
            string productID = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("SearchMaintainRecordDetail.aspx?{0}={1}", Constants.QUERYSTR_PRODUCT_ID, productID));
        }

        protected void yearDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindApplicatonEachMonth();
        }

        protected void maintainProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                ProductTypeMaintainModel model = (ProductTypeMaintainModel)e.Item.DataItem;
                string url = "http://chart.apis.google.com/chart?cht=bhs&chco=4d89f9,c6d9fd&chd=t:{0}|{1}&chs=100x25";
                url = string.Format(url, model.Persents, 100);
                ((Image)e.Item.FindControl("chart")).ImageUrl = url;
            }
        }
    }
}
